package com.zeyi.blog.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zeyi.blog.dto.Result;
import com.zeyi.blog.dto.StatusCode;
import com.zeyi.blog.entity.Comment;
import com.zeyi.blog.service.CommentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 文章评论表(Comment)表控制层
 *
 * @author 黎先森
 * @since 2020-07-31 15:32:09
 */
@RestController
@RequestMapping("comment")
public class CommentController {
    /**
     * 服务对象
     */
    @Resource
    private CommentService commentService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param comment 查询实体
     * @return 所有数据
     */
    @GetMapping
    public Result selectPage(Page<Comment> page, Comment comment) {
        return new Result(true, StatusCode.OK, "查询成功", this.commentService.page(page, new QueryWrapper<>(comment)));
    }

    /**
     * 新增数据
     *
     * @param comment 实体对象
     * @return 新增结果
     */
    @PostMapping
    public Result insert(@RequestBody Comment comment) {
        this.commentService.addComment(comment);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("{articleId}")
    public Result delete(@PathVariable String articleId, @RequestBody List<String> idList) {
        this.commentService.deleteComments(articleId, idList);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    /**
     * 获取文章评论
     * @param articleId
     * @return
     */
    @GetMapping("/article/{articleId}")
    public Result getArticleComments(@PathVariable String articleId) {
        return new Result(true, StatusCode.OK, "查询成功", this.commentService.getArticleComments(articleId));
    }
}